package org.example.admincontroller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Result;
import org.example.pojo.admindto.GoodsLookDTO;
import org.example.pojo.admindto.GoodsUpdateDTO;
import org.example.pojo.adminvo.GoodsVO;
import org.example.pojo.entity.Goods;
import org.example.adminservice.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/checkgoods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/save")
    public Result save(@RequestBody Goods goods) {
        goodsService.save(goods);
        return Result.success(goods);
    }

    @GetMapping("/list")
    public Result getgoods(@RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "10") int pageSize,
                           @RequestParam(value = "name", required = false) String name,
                           @RequestParam(value = "salenum", required = false) Integer salenum) {
        GoodsLookDTO goodsLookDTO=new GoodsLookDTO();
        goodsLookDTO.setName(name);
        goodsLookDTO.setSalenum(salenum);
        goodsLookDTO.setPage(page);
        goodsLookDTO.setPageSize(pageSize);
        if(goodsLookDTO.getPage()==null){
            goodsLookDTO.setPage(1);
        }
        if(goodsLookDTO.getPageSize()==null){
            goodsLookDTO.setPageSize(10);
        }
        GoodsVO goodsVO = new GoodsVO();
        goodsVO.setPageBean(goodsService.checkGoods(goodsLookDTO));
        return Result.success(goodsVO);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        goodsService.delete(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Goods goods) {
        log.info(goods.toString());
        goodsService.update(goods);
        return Result.success();
    }
}
